<?php
class Article extends AppModel{
    public $useTable  = 'article';
    public $primaryKey  = 'article_id';
    public $lang_code = 'vn';

    function add($data = array())
    {
        $this->save($data);
		return $this->getLastInsertId();
    }
    
    function get_article($status = "", $parent_id = null)
    {
        $status = $status == "all" ? '' : 'AND article.`status` = 1';
        $parent = $parent_id == null ? '' : 'AND article.category_id = '.$parent_id;
    	$lang_code = $this->lang_code;
        $sql = "SELECT article.*, article_desc.*, image.`name`
				FROM article_desc, article
                LEFT JOIN image ON article.article_id = image.post_id AND image.type = 'article' AND image.`status` = 1
				WHERE article.article_id = article_desc.article_id $parent $status AND article_desc.lang_code = '$lang_code' 
				ORDER BY article.sort_order, article_desc.title";

		return $this->query($sql);
    }
    
    function del($article_id = "")
    {
    	$sql = "DELETE FROM article WHERE article_id = '$article_id'";
    	$this->query($sql);
    	$sql = "DELETE FROM article_desc WHERE article_id = '$article_id'";
    	$this->query($sql);
        $sql = "DELETE FROM image WHERE post_id = '$article_id' AND type = 'article'";
    	$this->query($sql);
        $sql = "DELETE FROM link WHERE post_id = '$article_id' AND type = 'article'";
    	$this->query($sql);
    	return 1;
    }
    
    function change_status($article_id = "")
    {
    	$sql = "SELECT * FROM article WHERE article_id = '$article_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $status = $data[0]['article']['status'];
            $change_status = $status == 1 ? '0' : '1';
            $sql = "UPDATE article SET status = '$change_status' WHERE article_id = '$article_id'";
            $this->query($sql);
            return $change_status;
        }
        return -1;
    }
    
    public function get_article_by_id($article_id = "", $lang = '')
    {
    	if($article_id == "")
    		return null;
    	$lang_code = $this->lang_code;

    	$where_lang = "AND article_desc.lang_code = '$lang_code'";
    	if($lang == 'all')
    		$where_lang = "";

    	$result = array();
    	$sql = "SELECT * FROM article WHERE article_id = '$article_id'";
    	$data = $this->query($sql);
    	if(!empty($data))
    	{
    		$result['article'] = $data[0]['article'];
    	}
    	else
    	{
    		$result['article'] = array();
    	}
    	

    	$sql = "SELECT * 
				FROM  article_desc 
				WHERE article_id = '$article_id' $where_lang ";
		$data = $this->query($sql);
		$article_desc = array();
		foreach ($data as $value) {
			$lang_code = $value['article_desc']['lang_code'];
			$article_desc[$lang_code] = $value['article_desc'];

		}
		$result['article_desc'] = $article_desc;
        
        $where_lang = $lang == "all" ? "" : "AND lang_code = '$lang_code'";
            
        $sql = "SELECT * FROM link WHERE type = 'article' AND post_id = '$article_id' $where_lang ";
		$data = $this->query($sql);
		$link = array();
		foreach ($data as $value) {
			$lang_code = $value['link']['lang_code'];
			$link[$lang_code] = $value['link'];

		}
        $result['link'] = $link;
		return $result;
    }
    
    function edit($article_id = "", $data = array())
    {
		return $this->updateAll(
		    $data, 
		    array('article_id' => $article_id)
		);
    }
    
    function get_parent_category(){
    	$lang_code = $this->lang_code;
        $sql = "SELECT DISTINCT article.category_id, category_desc.title
                FROM article
                LEFT JOIN category_desc ON article.category_id = category_desc.category_id 
                AND category_desc.lang_code = '$lang_code'
                ORDER BY category_desc.title";
		$category = $this->query($sql);
        $list_category = array();
        foreach($category as $value)
        {
            $list_category[$value['article']['category_id']] = $value['category_desc']['title'];
        }
        return $list_category;
    }

}